import { Controller, Get, HttpException, Inject, Query } from '@nestjs/common';
import * as Minio from 'minio';

@Controller('minio')
export class MinioController {
  @Inject('MINIO_CLIENT')
  private minioClient: Minio.Client;

  @Get('presignedUrl')
  async getPresignedUrl(@Query('name') name: string) {
    try {
      // 编码文件名（关键修复）
      const url = await this.minioClient.presignedGetObject(
        'verynb',
        name, // 使用编码后的文件名
        3600,
      );

      console.log('Generated URL:', url);
      // 返回标准化URL（可选修复）
      return url;
    } catch (e) {
      throw new HttpException(e.message, 500);
    }
  }

  @Get('getUrl')
  async presignedGetObject(@Query('name') name: string) {
    // eslint-disable-next-line prefer-const
    let url = await this.minioClient.presignedGetObject('verynb', name, 3600);
    console.log('Generated Presigned URL:', url); // 打印生成的 URL
    return url;
  }
}
